package com.moon.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;





@Component
@RabbitListener(queues = "fanout.A")
public class SendEmailListenerA {

//    @Autowired
//    Producer producer;

    @Autowired
    JavaMailSender javaMailSender;

    @Autowired
    StringRedisTemplate stringRedisTemplate;


    @RabbitHandler
    public void process(Map emails, Channel channel, Message message) throws IOException {
        System.out.println("向邮箱发送邮件 "+ emails.get("userEmail").toString());

        //发送验证码邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("17605811811@163.com");
        mailMessage.setTo((String) emails.get("userEmail"));  //用自己的邮箱做测试
        mailMessage.setSubject("订单完成通知");
//        String code = producer.createText();
//        mailMessage.setText("您的注册验证码有效期为5分钟,请尽快使用 "+code);
        mailMessage.setText("您的订单已生成，可以打开蜗牛拍拍查看订单啦！");

        javaMailSender.send(mailMessage);

//        //redis中缓存验证码
//        stringRedisTemplate.opsForValue().set(email,code,5,TimeUnit.MINUTES);

//        //手动ack
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }

}
